﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using KylinORM.Utils;

namespace KylinORM.Aspect.Definition
{
    /// <summary>
    /// 实体字段信息
    /// </summary>
    public class EntityFieldInfo : EntityFieldAttribute, ICloneable<EntityFieldInfo>
    {
        /// <summary>
        /// 实体字段的引用
        /// </summary>
        public PropertyInfo PropertyInfo { get; internal set; }

        /// <summary>
        /// 获取指定实体的字段值
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        internal object GetValue(object entity)
        {
            return this.PropertyInfo.GetValue(entity, null);
        }
        /// <summary>
        /// 设置指定实体的字段值
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="value"></param>
        internal void SetValue(object entity, object value)
        {
            this.PropertyInfo.SetValue(entity, value, null);
        }

        EntityFieldInfo ICloneable<EntityFieldInfo>.Clone()
        {
            return new EntityFieldInfo
            {
                TableName = this.TableName,
                FieldName = this.FieldName,
                SelectMethod = ((ICloneable<ESelectMethod>)this.SelectMethod).Clone(),

                IsIgnoredField = this.IsIgnoredField,
                IsPrimaryField = this.IsPrimaryField,
                IsIdentityField = this.IsIdentityField,

                PropertyInfo = this.PropertyInfo
            };
        }

        object ICloneable.Clone()
        {
            return ((ICloneable<EntityFieldInfo>)this).Clone();
        }
    }
}
